/*!
 * 弹出输入控件.
 * ecp.qzz.ui.input.buttonedit.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.iedit"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui.input");
	/**
	 * 弹出输入控件.
	 */
	qzz.ui.input.buttonedit = qzz.Class(qzz.ui.input.edit, {
		/**
		 * 创建.
		 */
	    _create:function() {
	    	this.base();
		    this.className = "ButtonEdit";
		    this.logInfo("date.create");
		    //名称转换
		    this.transIDName = {};
		    //按钮
		    this.button = this.createElement("span");
		    //this.calendar = new qzz.ui.drop.PopCalendar(); 
	    },
	    /**
	     * 渲染.
	     */
	    _render:function(){
	    	this.base();	
	    	this.logInfo("date.render");
	    	this.setStyle(this.button, "componet_ui_input_showBotton");
	    	this._setHtml(this.button, "...");
            this.button._this = this;
            //弹出事件
            this.button.onclick = this.doButtonClick;
	    },
	    /**
	     * 按钮样式.
	     */
	    setButtonStyle:function(style) {
	    	if(style == "date") {
	    		style = "componet_ui_input_dateBotton";
	    	} else if(style == "drop") {
	    		style = "componet_ui_input_dropBotton";
	    	}
	    	this.setStyle(this.button, style);
	    },
	    /**
	     * 执行按钮事件.
	     */
	    doButtonClick : function() {
	    	var _this = this._this;
	    	if(_this._enabled === true && _this.getOption().readOnly !== true) {
		    	_this.logInfo("buttonEdit.click");
		    	if(typeof _this.onButtonClick == "function") {
		    		try {
		    		    _this.onButtonClick();
		    		} catch(e) {
		    			_this.logInfo("[onButtonClick]ERR:" + e.message);
		    		}
		    	}
		    	_this.trigger("onButtenClick", []);
	    	}
	    },
	    /**
	     * 执行推动焦点.
	     * @param ev 事件对象
	     */
	    doBlur:function(ev){
	    	this.base(ev);
	    },
	    /**
	     * 执行按键按下事件.
	     * @param ev 事件对象
	     * @param key 键值
	     */
	    doKeyDown:function(ev, key) {
	    	this.base(ev, key);
	    	if(key === this.keyBoard.F2) {
	    		this.button.onclick();
	    	}
	    },	
	    /**
	     * 执行提交.
	     */
	    postValue:function() {
	    	var text = this.inputBox.value;
	    	if(text === "" && this.notnull === true) return;
	    	if(typeof text == "string") {
	    		text = this._tagToText(text);
			}
	    	if(this.isEmpty(text)) {
	    		var re = {};
	    		re[this.getOption().idkey] = text;
	    		re[this.getOption().capkey] = text;
	    		this.setValue(re);
	    	} else {
	    		//如果事名称转换，则自动转换.
		    	var value = this._value;
		    	var hasValue = true;
		    	if(this.transIDName != null && this.transIDName[this._value] != text) {
		    		hasValue = false;
		    		for(var key in this.transIDName) {
		    			if(this.trim(this.transIDName[key]) == this.trim(text)) {
					    	value = key;
					    	hasValue = true;
					    	break;
					    }	
					}
		    		if(hasValue === false) {
		    			value = text;
		    		}
		    	}
		    	//处理过滤值不完整的时候是否可以输入.
		    	if(this.getOption().filterOnly !== true || hasValue === true) {
			    	var re = {};
		    		re[this.getOption().idkey] = value;
		    		re[this.getOption().capkey] = text;
			    	this.setValue(re);
		    	}
	    	}
	    },
	    refreash:function() {
	    	this.refresh();
	    },
	    /**
	     * 刷新.
	     */
	    refresh:function(){
	    	//缓存DOM元素结构，在IE下，不缓存，内容会丢失.
	    	this.docFragment.appendChild(this.button);
	    	this.base();
	    	this.inputBox.style.width = (this.getWidth() - 20) + "px";
	    	this.button.style.lineHeight = (this.getHeight()/2) + "px";
	    	this.thisWindow.appendChild(this.button);
	    	this._setHtml(this.button, "...");
	    },
	    /**
	     * 设置转换列表.
	     * @param inNameJson 转换列表.
	     */
	    setTransIDName:function(idNameJson) {
	    	this.transIDName = idNameJson;
	    },
	    /**
	     * 添加转换列表.
	     * @param inNameJson 转换列表.
	     */
	    addTransIDName:function(idNameJson) {
	    	var ojs = this.transIDName;
	    	if(ojs == null) {
	    		this.transIDName = idNameJson;
	    	} else if(idNameJson != null && idNameJson != null){
	    		for(var key in idNameJson) {
	    			ojs[key] = idNameJson[key];
	    		}
	    	}
	    	ojs = null;
	    },
	    setMultiSelect:function(value) {
	    	this.multiSelect = value;
	    }
	});
	
	/**
	 * 弹出编辑控件.
	 */
	_bindRenderFunc("qzzbuttonedit", qzz.ui.input.buttonedit);
	return qzz.ui.input.buttonedit;
});